package com.pic.others.mq;


import com.pic.common.constants.MqConstants;
import com.pic.others.config.minio.MinIOConfigProperties;
import com.pic.others.config.minio.MinIOUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
@Slf4j
public class PictureListerner {
    @Value("${minio.bucket}")
    String bucket;
    @Autowired
    private MinIOConfigProperties minIOConfigProperties;


    private final MinIOUtil minIOUtil;

    /**
     * 下载一张图片
     * @param locate
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.picture.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.PICTURE_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.PICTURE_DWONLOAD
    ))
    public void downLoadPicture(String locate){
        minIOUtil.downLoadFile(bucket,minIOConfigProperties.getReadPath()+locate);
    }

    /**
     * 下载多张图片
     * @param locates
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.picture.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.PICTURE_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.PICTURE_DWONLOADS
    ))
    public void downLoadPictures(List<String> locates){
       for(String locate:locates){
           minIOUtil.downLoadFile(bucket,minIOConfigProperties.getReadPath()+locate);
       }
    }

}
